//
//  SQVisitorView.swift
//  SwiftWeibo
//
//  Created by suqianghotel on 16/11/19.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit

class SQVisitorView: UIView {
    
    
    var visitorInfo: [NSString:NSString]?
    {
        didSet{
            guard
                let imageName = visitorInfo?["imageName"],
                let text = visitorInfo?["text"]
            else {
                return;
            }
            
            houseView.image = UIImage(named: imageName as String)
            textLabel.text = text as String
        }
    }

    lazy var imageView : UIImageView = {
        var view = UIImageView()
        view.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return view
    }()
    
    private lazy var imageMaskView : UIView = UIView()
    
    
    private lazy var houseView: UIImageView = {
        var view = UIImageView()
        view.image = UIImage(named: "visitordiscover_feed_image_house")
        return view
    }()
    
    
    private lazy var textLabel : UILabel = {
        var label = UILabel()
        label.font = UIFont .systemFont(ofSize: 12)
        label.numberOfLines = 0
        label.textColor = UIColor.darkGray.withAlphaComponent(0.5)
        label.textAlignment = .center
        label.text = "在之前的帖子里聊过状态管理有多痛苦,有时这是不可避免的"
        return label
    }()
    
    
    lazy var registerButton : UIButton = {
        var button = UIButton()
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named:"common_button_big_white")?.horizonResize(), for: .normal)
        button.setBackgroundImage(UIImage(named:"common_button_big_white_highlighted")?.horizonResize(), for: .highlighted)
        
        return button;
    }()
    
    lazy var loginButton : UIButton = {
        var button = UIButton()
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named:"common_button_big_white")?.horizonResize(), for: .normal)
        button.setBackgroundImage(UIImage(named:"common_button_big_white_highlighted")?.horizonResize(), for: .highlighted)
        
        return button;
    }()
    
    lazy var attentionButton : UIButton = {
        var button = UIButton()
        button.setTitle("去关注", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named:"common_button_big_white")?.horizonResize(), for: .normal)
        button.setBackgroundImage(UIImage(named:"common_button_big_white_highlighted")?.horizonResize(), for: .highlighted)
        
        return button;
    }()
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
//        addRotationAnimation(view: imageView)
        addMask(view: imageMaskView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setupUI()
//        addRotationAnimation(view: imageView)
    }
    
    private func setupUI()
    {
        addSubview(imageView)
        addSubview(imageMaskView)
        addSubview(houseView)
        addSubview(textLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(attentionButton)
        

        attentionButton.isHidden = true
        imageView.isHidden = true
        
        autoresizesSubviews = false
        
        for view in subviews
        {
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 背景图
        addConstraint(NSLayoutConstraint(item: imageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: imageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1,
                                         constant: -80))
        
        
        // 遮罩
        addConstraint(NSLayoutConstraint(item: imageMaskView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerY,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: imageMaskView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: imageMaskView,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .width,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: imageMaskView,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .height,
                                         multiplier: 1,
                                         constant: 0))
        
        // 小房子
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerY,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        
        
        // 提示Label
        addConstraint(NSLayoutConstraint(item: textLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: textLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .width,
                                         multiplier: 0.65,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: textLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: imageView,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 0))
        
        
        // 注册按钮
        
        let btnH : CGFloat = 30
        let btnW : CGFloat = btnH * 3
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: textLabel,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: 20))
        
//        addConstraint(NSLayoutConstraint(item: registerButton,
//                                         attribute: .height,
//                                         relatedBy: .equal,
//                                         toItem: nil,
//                                         attribute: .notAnAttribute,
//                                         multiplier: 1,
//                                         constant: btnH)) //
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1,
                                         constant: btnW))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: textLabel,
                                         attribute: .left,
                                         multiplier: 1,
                                         constant: 10))
        
        
        
        // 登录按钮 loginButton
//        addConstraint(NSLayoutConstraint(item: loginButton,
//                                         attribute: .height,
//                                         relatedBy: .equal,
//                                         toItem: registerButton,
//                                         attribute: .height,
//                                         multiplier: 1,
//                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: textLabel,
                                         attribute: .right,
                                         multiplier: 1,
                                         constant: -10))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .top,
                                         multiplier: 1,
                                         constant: 0))
        
        
        // 去关注
        addConstraint(NSLayoutConstraint(item: attentionButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .top,
                                         multiplier: 1,
                                         constant: 0))
        
//        addConstraint(NSLayoutConstraint(item: attentionButton,
//                                         attribute: .height,
//                                         relatedBy: .equal,
//                                         toItem: registerButton,
//                                         attribute: .height,
//                                         multiplier: 1,
//                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: attentionButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: attentionButton,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: textLabel,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        
        
        
        
    }
    
    
    private func addRotationAnimation(view: UIView) {
        
        let anim = CABasicAnimation(keyPath:"transform.rotation.z")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.repeatDuration = 15.0
        
        anim.isRemovedOnCompletion = false
        
        view.layer.add(anim, forKey: nil)
    }
    
    private func addMask(view: UIView)
    {
        let gradientLayer = CAGradientLayer()
        
        let color1 = UIColor.clear.withAlphaComponent(1).cgColor
        let color11 = UIColor.clear.withAlphaComponent(0.9).cgColor
        let color2 = UIColor.clear.withAlphaComponent(0.8).cgColor
        let color3 = UIColor.clear.withAlphaComponent(0.1).cgColor
        let color4 = UIColor.clear.withAlphaComponent(0).cgColor
        gradientLayer.colors = [color1,color11, color2, color3, color4]
        
        gradientLayer.locations = [0, 0.3, 0.5, 0.8, 1]
        
        gradientLayer.frame = CGRect(x: 0, y: 0, width: (imageView.image?.size.height)!, height: (imageView.image?.size.width)!)
//        view.layer.mask = gradientLayer
//        view.layer.addSublayer(gradientLayer)
        

    }
    
    
    
}
